package org.yu.api.register;

import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.TieredItem;
import net.minecraftforge.client.model.generators.ItemModelProvider;

import javax.annotation.ParametersAreNonnullByDefault;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;

@ParametersAreNonnullByDefault
@SuppressWarnings("unused")
public  class ItemBuilder<T extends Item> {
    private final String name;
    private final LazyRegistry lazyRegistry;
    private final Function<Item.Properties, T> itemFunc;
    private final Map<ItemHolder<?>, BiConsumer<ItemModelProvider, ItemHolder<?>>> iItemModelMap;
    private final List<TagKey<Item>> tags = new ArrayList<>();
    private final HashMap<String, String> langMap = new HashMap<>();
    private Item.Properties properties;
    private BiConsumer<ItemModelProvider, ItemHolder<?>> iItemModel;
    private CreativeModeTab tab;
    public static final ResourceLocation HANDHELD = new ResourceLocation("item/handheld");

    protected ItemBuilder(LazyRegistry lazyRegistry, String name, Function<Item.Properties, T> itemFunc, Map<ItemHolder<?>, BiConsumer<ItemModelProvider, ItemHolder<?>>> iItemModelMap) {
        this.name = name;
        this.lazyRegistry = lazyRegistry;
        this.itemFunc = itemFunc;
        this.iItemModelMap = iItemModelMap;
    }

    public ItemBuilder<T> propertie(Consumer<Item.Properties> consumer) {
        if (properties == null) properties = new Item.Properties();
        consumer.accept(properties);
        return this;
    }
    public ItemBuilder<T> model(BiConsumer<ItemModelProvider, ItemHolder<?>> iItemModel) {
        this.iItemModel = iItemModel;
        return this;
    }

    public ItemBuilder<T> tab(CreativeModeTab tab) {
        this.tab = tab;
        return this;
    }

    public ItemBuilder<T> tag(TagKey<Item> tagKey) {
        this.tags.add(tagKey);
        return this;
    }

    public ItemBuilder<T> name(String lang, String value) {
        langMap.put(lang, value);
        return this;
    }

    /**
     * @param value 本地化默认名字
     * @return ItemBuilder
     */
    public ItemBuilder<T> defaultName(String value) {
        return name(lazyRegistry.defaultLang, value);
    }

    public ItemHolder<T> build() {
        if (properties == null) properties = new Item.Properties();
        var result = new ItemHolder<>(lazyRegistry.items.register(name, () -> itemFunc.apply(properties)));
        properties.tab(Objects.requireNonNullElse(tab, lazyRegistry.creativeModeTab));
        tags.forEach(tagKey -> lazyRegistry.itemTagMap.put(result, tagKey));
        iItemModelMap.put(result, Objects.requireNonNullElseGet(iItemModel, () -> (itemModelProvider, itemHolder) -> {
            var id=itemHolder.get().getRegistryName();
           if(itemHolder.get() instanceof TieredItem)
               itemModelProvider.withExistingParent(id.getPath(), HANDHELD).texture("layer0", new ResourceLocation(id.getNamespace(), "item/" + id.getPath()));
               else
            itemModelProvider.basicItem(itemHolder.get());
        }));
        if (!langMap.containsKey(lazyRegistry.defaultLang)) defaultName(LangBuilder.toTitleCase(name));
        langMap.forEach((lang, value) -> lazyRegistry.lang(lang, () -> new TranslationBean(result.get().getDescriptionId(), value)));
        return result;
    }

}
